﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Util;

namespace DAO
{
    /// <summary>
    /// Summary description for Module
    /// </summary>
    public class Module
    {
        DBConnection DBConnection = new DBConnection();
        public Module()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// Get all DTO.Module instance from DB.
        /// </summary>
        /// <returns>List<> of all DTO.Module instances.</returns>
        public List<DTO.Module> GetAllModules()
        {
            Log.LogInfo("Begin GetAllModules()");
            String query = "SELECT * FROM [Module];";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            List<DTO.Module> moduleDTOs = new List<DTO.Module>();
            try
            {
                while (reader.Read())
                {
                    moduleDTOs.Add(new DTO.Module(reader));
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetAllModules(): " + ex.StackTrace);
            }
            DBConnection.Close();
            Log.LogInfo("End GetAllModules()");
            return moduleDTOs;
        }
        /// <summary>
        /// Get DTO.Module instance from DB.
        /// </summary>
        /// <param name="ID">Module ID</param>
        /// <returns>DTO.Module instance which has the specific ID.</returns>
        public DTO.Module GetModule(String ID)
        {
            Log.LogInfo("Begin GetModule(String ID)");
            String query = "SELECT * FROM [Module] WHERE [Module].[ID]='" + ID + "';";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            DTO.Module moduleDTO = null;
            try
            {
                if (reader.Read())
                    moduleDTO = new DTO.Module(reader);
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetModule(String ID): " + ex.Message);
            }

            Log.LogInfo("End GetModule(String ID)");
            DBConnection.Close();
            return moduleDTO;
        }

        /// <summary>
        /// Insert Module into database.
        /// </summary>
        /// <param name="Module">DTO.Module instance to insert into database.</param>
        public int InsertModule(DTO.Module Module)
        {
            Log.LogInfo("Begin InsertModule()");
            String query = "";
            query += "INSERT INTO MODULE ( Name, Description, Icon, FilePath) VALUES ('"  + Module.Name + "','" + Module.Description + "','" + Module.IconPath + "','" + Module.FilePath + "')";
            query += "";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Insert " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertModule()");
            return result;
        }

        /// <summary>
        /// Update Module info into database.
        /// </summary>
        /// <param name="Module">Module instance to be updated.</param>
        public int UpdateModule(DTO.Module Module)
        {
            Log.LogInfo("Begin UpdateModule()");
            String query = "UPDATE MODULE SET Name = '" + Module.Name + "'," + " Description = '" + Module.Description + "'Icon ='" + Module.IconPath + "'FilePath = '" + Module.FilePath + "' WHERE ID = '" + Module.FilePath + "'";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Update " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertModule()");
            return result;
        }
        /// <summary>
        /// Delete a specific Module from DB.
        /// </summary>
        /// <param name="Module">The Module to be deleted.</param>
        public int DeleteModule(DTO.Module Module)
        {
            Log.LogInfo("Begin DeleteModule()");
            String query = "DELETE FROM [Module] WHERE [Module].[ID]='" + Module.ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteModule()");
            return result;
        }

        /// <summary>
        /// Delete a Module in database using ID.
        /// </summary>
        /// <param name="ID">Module ID.</param>
        public int DeleteModule(String ID)
        {
            Log.LogInfo("Begin DeleteModule()");
            String query = "DELETE FROM [Module] WHERE [Module].[ID]='" + ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteModule()");
            return result;
        }
    }
}